package com.jmz.apt.api.Utils;

import com.google.common.collect.Maps;
import com.jmz.apt.api.enums.ResponseCode;
import com.jmz.apt.api.message.response.ResponseMessage;
import com.jmz.apt.api.message.response.ResponseMessageTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * Created by fanglexue on 2019/10/25.
 */
@Component
@Slf4j
public class ResponseUtil {

    public void errorAuthentication(HttpServletResponse response, String errorMsg) {
        PrintWriter out=null;
        try {
            Map<String,Object> data= Maps.newConcurrentMap();
            data.put("error",errorMsg);
            response.setCharacterEncoding("UTF-8");
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());

            ResponseMessageTemplate responseMessageTemplate = new ResponseMessageTemplate();
            responseMessageTemplate.setBussResponse(ResponseCode.AUTHENTICATION_FAILURE.getCode(),errorMsg);
            ResponseMessage responseResult = responseMessageTemplate.getResponseMessage();
            log.info(JsonSerializerUtil.obj2String(responseResult));
            out=response.getWriter() ;
            out.write(JsonSerializerUtil.obj2String(responseResult));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            out.close();
        }


    }
    public  void warningAuthentication(HttpServletResponse response, String warningMsg) {
        PrintWriter out=null;
        try {
            Map<String,Object> data= Maps.newConcurrentMap();
            data.put("error",warningMsg);
            response.setCharacterEncoding("UTF-8");
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            //response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
            //ResponseResult responseResult= ResponseResult.createByError(ResponseCode.AUTHENTICATION_FAILURE.getCode()
                    //,ResponseCode.AUTHENTICATION_FAILURE.getDesc(),data);
            ResponseMessageTemplate responseMessageTemplate = new ResponseMessageTemplate();
            responseMessageTemplate.setBussResponse(ResponseCode.AUTHENTICATION_FAILURE.getCode(),warningMsg);
            ResponseMessage responseResult = responseMessageTemplate.getResponseMessage();
            out=response.getWriter() ;
            out.write(JsonSerializerUtil.obj2String(responseResult));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            out.close();
        }

    }




    public  void warningAccessDenied(HttpServletResponse response, String warningMsg) {
        PrintWriter out=null;
        try {
            Map<String,Object> data= Maps.newConcurrentMap();
            data.put("error",warningMsg);
            response.setCharacterEncoding("UTF-8");
            response.setStatus(HttpStatus.FORBIDDEN.value());
            //response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
            //ResponseResult responseResult= ResponseResult.createByError(ResponseCode.AUTHENTICATION_FAILURE.getCode()
             //       ,ResponseCode.AUTHENTICATION_FAILURE.getDesc(),data);

            ResponseMessageTemplate responseMessageTemplate = new ResponseMessageTemplate();
            responseMessageTemplate.setBussResponse(ResponseCode.AUTHENTICATION_FAILURE.getCode(),warningMsg);
            ResponseMessage responseResult = responseMessageTemplate.getResponseMessage();
            out=response.getWriter() ;
            out.write(JsonSerializerUtil.obj2String(responseResult));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            out.close();
        }
    }
}
